/*
 * Created by JFormDesigner on Thu Dec 12 23:30:55 CST 2024
 */

package com.hafu.view.ShangPin;

import java.awt.event.*;
import com.hafu.model.SpInfo;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author Brainrain
 */
public class ManageShangPinFrm extends JFrame {
    private SpInfoService spInfoService = new SpInfoService();
    public ManageShangPinFrm() {
        initComponents();
        fillTable();
    }
    private void fillTable() {
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        ArrayList<SpInfo> spInfos = spInfoService.getSpInfoByName(new SpInfo());
        for (SpInfo spInfo : spInfos){
            Object[] rowData = {spInfo.getId(),spInfo.getName(),spInfo.getPrice(),spInfo.getSellPrice()};
            dtm.addRow(rowData);
        }
    }

    private void textField1(ActionEvent e) {
        // TODO add your code here
    }
    private void table1MouseClicked(MouseEvent e) {
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        String name = (String)dtm.getValueAt(row, 1);
        BigDecimal price = (BigDecimal)dtm.getValueAt(row, 2);
        BigDecimal sellPrice = (BigDecimal)dtm.getValueAt(row, 3);
        textField2.setText(name);
        textField3.setText(price.toString());
        textField4.setText(sellPrice.toString());
    }

    private void button1(ActionEvent e) {
        // TODO add your code here
        String name = textField1.getText();
        SpInfo spInfo = new SpInfo(name);
        ArrayList<SpInfo> spInfos = spInfoService.getSpInfoByName(spInfo);
                //.getSpInfoByName(spInfo);
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        if (spInfos.size() > 0) {
            for (SpInfo spInfo1 : spInfos) {
                Object[] rowData = {spInfo1.getId(), spInfo1.getName(), spInfo1.getPrice(), spInfo1.getSellPrice()};
                dtm.addRow(rowData);
            }
        }else {
            JOptionPane.showMessageDialog(null,"没有该商品信息");
        }
    }

    private void button2(ActionEvent e) {
        // TODO add your code here
        String name = textField2.getText();
        String price = textField3.getText();
        String sellPrice = textField4.getText();
        if (StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"商品名称不能为空");
            return;
        }
        if (StringUtil.isEmpty(price)){
            JOptionPane.showMessageDialog(null,"商品进价不能为空");
            return;
        }
        if (StringUtil.isEmpty(sellPrice)) {
            JOptionPane.showMessageDialog(null, "商品售价不能为空");
            return;
        }
        BigDecimal price1 = new BigDecimal(price);
        BigDecimal sellPrice1 = new BigDecimal(sellPrice);
        int row = table1.getSelectedRow();
        //获取行的数据
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row,0);
        SpInfo spInfo = new SpInfo(chooseId,name,price1,sellPrice1);
        Boolean aBoolean = spInfoService.updateSpInfo(spInfo);
        if (aBoolean) {
            JOptionPane.showMessageDialog(null, "修改成功");
            fillTable();
        }else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }

    private void button3(ActionEvent e) {
        // TODO add your code here
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int id = (int) dtm.getValueAt(row, 0);
        Boolean isDeleteSpInfo = spInfoService.deleteSpInfo(id);
        if (isDeleteSpInfo) {
            JOptionPane.showMessageDialog(null, "删除成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null, "删除失败");
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        textField4 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();

        //======== this ========
        setTitle("\u5546\u54c1\u7ba1\u7406");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5546\u54c1\u540d\u5b57");

        //---- textField1 ----
        textField1.addActionListener(e -> textField1(e));

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null, null},
                },
                new String[] {
                    "\u5546\u54c1\u7f16\u53f7", "\u5546\u54c1\u540d\u79f0", "\u5546\u54c1\u8fdb\u4ef7", "\u5546\u54c1\u552e\u4ef7"
                }
            ));
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u5546\u54c1\u540d\u5b57");

        //---- label3 ----
        label3.setText("\u5546\u54c1\u8fdb\u4ef7");

        //---- label4 ----
        label4.setText("\u5546\u54c1\u552e\u4ef7");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(38, 38, 38)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 498, GroupLayout.PREFERRED_SIZE)
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(label1)
                                    .addGap(64, 64, 64)
                                    .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 286, GroupLayout.PREFERRED_SIZE)
                                    .addGap(79, 79, 79)
                                    .addComponent(button1))))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(62, 62, 62)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label2)
                                .addComponent(label3)
                                .addComponent(label4))
                            .addGap(120, 120, 120)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                                .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                                .addComponent(textField4, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
                            .addGap(117, 117, 117)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(button2)
                                .addComponent(button3))))
                    .addContainerGap(155, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(16, 16, 16)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label1)
                                .addComponent(button1)
                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 282, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label2))
                            .addGap(14, 14, 14)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label3))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addContainerGap(364, Short.MAX_VALUE)
                            .addComponent(button2)
                            .addGap(36, 36, 36)))
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label4))
                            .addGap(49, 49, 49))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addComponent(button3)
                            .addGap(58, 58, 58))))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JButton button2;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
